home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / test / doublebuffer.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  4.0 KB  |  127 lines

  1. //
  2. // "$Id: doublebuffer.cxx,v 1.4 1999/01/07 19:17:52 mike Exp $"
  3. //
  4. // Double-buffering test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // This demo shows how double buffering helps, by drawing the
  7. // window in a particularily bad way.
  8. //
  9. // The single-buffered window will blink as it updates.  The
  10. // double buffered one will not.  It will take just as long
  11. // (or longer) to update, but often it will appear to be faster.
  12. //
  13. // This demo should work for both the GL and X versions of Fl,
  14. // even though the double buffering mechanism is totally different.
  15. //
  16. // Copyright 1998-1999 by Bill Spitzak and others.
  17. //
  18. // This library is free software; you can redistribute it and/or
  19. // modify it under the terms of the GNU Library General Public
  20. // License as published by the Free Software Foundation; either
  21. // version 2 of the License, or (at your option) any later version.
  22. //
  23. // This library is distributed in the hope that it will be useful,
  24. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  26. // Library General Public License for more details.
  27. //
  28. // You should have received a copy of the GNU Library General Public
  29. // License along with this library; if not, write to the Free Software
  30. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  31. // USA.
  32. //
  33. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  34. //
  35.  
  36. #include <FL/Fl.H>
  37. #include <FL/Fl_Single_Window.H>
  38. #include <FL/Fl_Double_Window.H>
  39. #include <FL/Fl_Box.H>
  40. #include <FL/fl_draw.H>
  41. #include <FL/Fl_Hor_Slider.H>
  42. #include <stdlib.h>
  43. #include <FL/math.h>
  44. #include <stdio.h>
  45.  
  46. // this purposely draws each line 10 times to be slow:
  47. void star(int w, int h, int n) {
  48.   fl_push_matrix();
  49.   fl_translate(w/2, h/2);
  50.   fl_scale(w/2, h/2);
  51.   for (int i = 0; i < n; i++) {
  52.     for (int j = i+1; j < n; j++)/* for (int k=0; k<10; k++)*/ {
  53.       fl_begin_line();
  54.       fl_vertex(cos(2*M_PI*i/n+.1), sin(2*M_PI*i/n+.1));
  55.       fl_vertex(cos(2*M_PI*j/n+.1), sin(2*M_PI*j/n+.1));
  56.       fl_end_line();
  57.     }
  58.   }
  59.   fl_pop_matrix();
  60. }
  61.  
  62. int sides[2] = {20,20};
  63.  
  64. void slider_cb(Fl_Widget* o, long v) {
  65.   sides[v] = int(((Fl_Slider*)o)->value());
  66.   o->parent()->redraw();
  67. }
  68.  
  69. void bad_draw(int w,int h,int which) {
  70. //   for (int i=0; i<10; i++) {
  71. //     fl_color(7); fl_rectf(0,0,w,h); fl_color(0); star(w,h);
  72. //     fl_color(0); fl_rectf(0,0,w,h); fl_color(7); star(w,h);
  73. //   }
  74.   fl_color(FL_BLACK); fl_rectf(0,0,w,h);
  75.   fl_color(FL_WHITE); star(w,h,sides[which]);
  76.   //  for (int x=0; x<sides[which]; x++) for (int y=0; y<sides[which]; y++)
  77.   //fl_draw_box(FL_UP_BOX, 10*x, 10*y, 25,25, FL_GRAY);
  78. }
  79.  
  80. class single_blink_window : public Fl_Single_Window {
  81.   void draw() {bad_draw(w(),h(),0); draw_child(*child(0));}
  82. public:
  83.   single_blink_window(int x, int y,int w,int h,const char *l)
  84.     : Fl_Single_Window(x,y,w,h,l) {resizable(this);}
  85. };
  86.  
  87. class double_blink_window : public Fl_Double_Window {
  88.   void draw() {bad_draw(w(),h(),1); draw_child(*child(0));}
  89. public:
  90.   double_blink_window(int x, int y, int w,int h,const char *l)
  91.     : Fl_Double_Window(x,y,w,h,l) {resizable(this);}
  92. };
  93.  
  94. int main() {
  95.   if (!Fl::visual(FL_DOUBLE))
  96.     printf("Xdbe not supported, faking double buffer with pixmaps.\n");
  97.   Fl_Window w01(420,420,"Fl_Single_Window"); w01.box(FL_FLAT_BOX);
  98.   single_blink_window w1(10,10,400,400,"Fl_Single_Window");
  99.   w1.box(FL_FLAT_BOX); w1.color(FL_BLACK); //w1.position(100,200);
  100.   Fl_Hor_Slider slider0(20,370,360,25);
  101.   slider0.range(2,30);
  102.   slider0.step(1);
  103.   slider0.value(sides[0]);
  104.   slider0.callback(slider_cb, 0);
  105.   w1.end();
  106.   w01.end();
  107.   Fl_Window w02(420,420,"Fl_Double_Window"); w02.box(FL_FLAT_BOX);
  108.   double_blink_window w2(10,10,400,400,"Fl_Double_Window");
  109.   w2.box(FL_FLAT_BOX); w2.color(FL_BLACK); //w2.position(600,200);
  110.   Fl_Hor_Slider slider1(20,370,360,25);
  111.   slider1.range(2,30);
  112.   slider1.step(1);
  113.   slider1.value(sides[0]);
  114.   slider1.callback(slider_cb, 1);
  115.   w2.end();
  116.   w02.end();
  117.   w01.show();
  118.   w1.show();
  119.   w02.show();
  120.   w2.show();
  121.   return Fl::run();
  122. }
  123.  
  124. //
  125. // End of "$Id: doublebuffer.cxx,v 1.4 1999/01/07 19:17:52 mike Exp $".
  126. //
  127.